package cn.dwx.demo6.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    /*@Override //用来配置拦截保护的请求，比如什么请求放行，什么请求需要验证,http为安全请求对象
    protected void configure(HttpSecurity http) throws Exception{
        super.configure(http);
    }
    @Override //用来配置用户签名服务，主要是user-details机制，可以给用户赋予角色，@param auth签名构造管理器，用于构建用户具体权限控制
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        super.configure(auth);
        auth.inMemoryAuthentication()
                .withUser("dwx").password("dwx").roles("ROLE_ADMIN")
                .and()
                .withUser("wisely").password("wisely").roles("ROLE_USER");

    }
    @Override //用来配置Filter链，配置忽略哪些内容，web为web security对象
    public void configure(WebSecurity web) throws Exception{
        super.configure(web);
    }*/

    //----------------------------------------------------分割线-------------------------------------------------------//

    @Override //用来配置用户签名服务，主要是user-details机制，可以给用户赋予角色，@param auth签名构造管理器，用于构建用户具体权限控制
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        super.configure(auth);
        auth.inMemoryAuthentication()
                .withUser("dwx").password("dwx").roles("ADMIN")
                .and()
                .withUser("wisely").password("wisely").roles("USER");

    }
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                    .loginPage("/login2")
                    .successForwardUrl("/login2_success")
                   // .failureUrl("/login2?error")
                    .permitAll()
                .and()
                .logout().permitAll();
    }

}
